﻿using PLC.Interface.BaseUsers;
using PLCWpfTest.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using YWM;
using YWM.Dto;
using YWM.Localization;

namespace PLCWpfTest.Common
{
    public abstract class BaseViewModel : EmptyDto
    {
        /// <summary>
        /// 微软雅黑
        /// </summary>
        protected const string FONT_FAMILY_NAME_MICROSOFT_YAHEI = "Microsoft YaHei";

        /// <summary>
        /// 是否是管理员
        /// </summary>
        protected bool IsAdmin
        {
            get
            {
                return LoginUserInfo?.Account.Equals(Constant.ADMIN) == true;
            }
        }

        /// <summary>
        /// 登陆用户信息
        /// </summary>
        private BaseUserResultDto _loginUserInfo;
        public BaseUserResultDto LoginUserInfo
        {
            get { return _loginUserInfo; }
            set { SetProperty(ref _loginUserInfo, value); }
        }

        /// <summary>
        /// 加载命令
        /// </summary>
        public AsyncRelayCommand LoadCommand { get; set; }

        /// <summary>
        /// 语言模型对象数组
        /// </summary>
        public NameValueModel<string>[] CultureModels { get; set; } = new NameValueModel<string>[]
        {
            new NameValueModel<string>("中文简体",Constant.LANG_ZH_CN),
            new NameValueModel<string>("English",Constant.LANG_EN_US)
        };

        /// <summary>
        /// 语言管理对象
        /// </summary>
        private ILocalizationManager _lang = YApp.Resolve<ILocalizationManager>();
        public ILocalizationManager Lang
        {
            get { return _lang; }
            set { SetProperty(ref _lang, value); }
        }

        private event Action _requestClose;
        /// <summary>
        /// 请求关闭事件
        /// </summary>
        public event Action RequestClose
        {
            add { _requestClose += value; }
            remove { _requestClose -= value; }
        }

        public BaseViewModel()
        {
            LoginUserInfo = DataCache.LoginUserInfo;
            DataCache.OnUpdateLoginUser += DataCache_OnUpdateLoginUser;
            LoadCommand = YApp.Resolve<AsyncRelayCommand>();
            LoadCommand.BeforeCanExecute += LoadCommand_BeforeCanExecute;
            LoadCommand.BeforeExecute += LoadCommand_BeforeExecute;
            LoadCommand.Executing += LoadCommand_Executing;
        }

        /// <summary>
        /// 数据缓存更新登录用户事件
        /// </summary>
        /// <param name="userInfo"></param>
        private void DataCache_OnUpdateLoginUser(BaseUserResultDto userInfo)
        {
            LoginUserInfo = userInfo;
        }

        /// <summary>
        /// 加载命令可执行之前事件
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        protected virtual bool LoadCommand_BeforeCanExecute(object arg)
        {
            return true;
        }

        /// <summary>
        /// 加载命令执行之前事件
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        protected virtual bool LoadCommand_BeforeExecute(object parameter)
        {
            return true;
        }

        /// <summary>
        /// 加载命令执行中事件
        /// </summary>
        /// <param name="parameter"></param>
        protected virtual Task LoadCommand_Executing(object parameter)
        {
            return Task.CompletedTask;
        }

        /// <summary>
        /// 设置语言
        /// </summary>
        public void SetLang()
        {
            List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }
            string requestedCulture = $"pack://application:,,,/PLCWpfTest;component/Resources/I18N/{Lang.Culture}.xaml";
            ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
            if (resourceDictionary != null)
            {
                Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
            CultureInfo culture = new CultureInfo(Lang.Culture);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
            Lang = YApp.Resolve<ILocalizationManager>();
        }

        /// <summary>
        /// 请求关闭方法
        /// </summary>
        protected virtual void RaiseRequestClose()
        {
            _requestClose?.Invoke();
        }
    }
}
